#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"

#include "iot_errno.h"
#include "iot_uart.h"
#include "hi_uart.h"
#include "hi_io.h"
#include "hi_gpio.h"

#define UART_BUFF_SIZE 1024

#define UART_PORT 1


unsigned int IoTUartInit(unsigned int id, const IotUartAttribute *param)
{
    hi_uart_attribute attr = {0};
    hi_uart_extra_attr extraAttr = {0};

    if (NULL == param) {
        return IOT_FAILURE;
    }

    attr.baud_rate = param->baudRate;
    attr.data_bits = param->dataBits;
    attr.pad = param->pad;
    attr.parity = param->parity;
    attr.stop_bits = param->stopBits;

    if (IOT_UART_BLOCK_STATE_NONE_BLOCK == param->rxBlock) {
        extraAttr.rx_block = HI_UART_BLOCK_STATE_NONE_BLOCK;
    } else {
        extraAttr.rx_block = HI_UART_BLOCK_STATE_BLOCK;
    }

    if (IOT_UART_BLOCK_STATE_NONE_BLOCK == param->txBlock) {
        extraAttr.tx_block = HI_UART_BLOCK_STATE_NONE_BLOCK;
    } else {
        extraAttr.tx_block = HI_UART_BLOCK_STATE_BLOCK;
    }

    return hi_uart_init((hi_uart_idx)id, &attr, &extraAttr);
}


static void UART_Task(void)
{
    uint32_t ret;
    char uart_buff[UART_BUFF_SIZE] = {0};
    static const char *data = "Hello, SPARK-HM-PI!\r\nStar to receive messages!\r\n";

    IotUartAttribute uart_attr = {
        //波特率115200d、数据位8、停止位1、奇偶检验位无
        .baudRate = 115200,
        .dataBits = 8,
        .stopBits = 1,
        .parity = 0,
    };

    //复用引脚功能为串口，GPIO5,6,默认就是串口功能可不进行复用
    // IoSetFunc(WIFI_IOT_IO_NAME_GPIO_5, WIFI_IOT_IO_FUNC_GPIO_5_UART1_RXD);
    // IoSetFunc(WIFI_IOT_IO_NAME_GPIO_6, WIFI_IOT_IO_FUNC_GPIO_6_UART1_TXD);

    //初始化串口
    ret = IoTUartInit(UART_PORT, &uart_attr);
    //hi_uart_init(UART_PORT, &uart_attr,&extraAttr);
    //ret = hi_uart_init(DEMO_UART_NUM, &uart_attr, HI_NULL);
    if (ret != IOT_SUCCESS)
    {
        printf("Failed to init uart%d.", UART_PORT);
        return;
    }

    //IoTUartWrite(UART_PORT, (unsigned char *)data, strlen(data));
    hi_uart_write(UART_PORT, (unsigned char *)data, strlen(data));

    printf("UART Init Success\n");
    while (1)
    {
        //清空buffer
        bzero(uart_buff,sizeof(uart_buff));

        //阻塞接收串口消息
        //IoTUartRead(UART_PORT, (unsigned char *)uart_buff, sizeof(uart_buff));
        hi_uart_read(UART_PORT, (unsigned char *)uart_buff, sizeof(uart_buff));
        printf("Uart1 read data:%s", uart_buff);

        //将接收到的消息重新发送
        //IoTUartWrite(UART_PORT, (unsigned char *)uart_buff, strlen(uart_buff));
        hi_uart_write(UART_PORT, (unsigned char *)uart_buff, strlen(uart_buff));
        
        usleep(100000);
    }
}

//创建Uart线程
static void UART_DemoEntry(void)
{

    osThreadAttr_t attr;

    attr.name = "UART_Task";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 1024*8;
    attr.priority = 25;

    if (osThreadNew((osThreadFunc_t)UART_Task, NULL, &attr) == NULL)
    {
        printf("[UART_Example] Falied to create UART_Task!\n");
    }
}

SYS_RUN(UART_DemoEntry);